<?php

class Settings {
    
	const DEFAULTS	= 0;
	const SESSION	= 1;
    const DATABASE	= 2;
    const CONFIG	= 3;
    
	protected static $_target = array(
		'children' => array(
			'fields' => self::DEFAULTS
		),
        'post' => array(
			'paginator' => array(
				'page_range' => self::DEFAULTS,
				'item_count_per_page' => self::DEFAULTS
			),
			'preview' => array(
				'length' => self::DEFAULTS
			)
        ),
        'news' => array(
			'paginator' => array(
				'page_range' => self::DEFAULTS,
				'item_count_per_page' => self::DEFAULTS
			),
			'preview' => array(
				'length' => self::DEFAULTS
			),
			'rss' => array(
				'count' => self::DEFAULTS
			)
        ),
		'site' => array(
			'ajax' => array(
				'protected_error_message' => self::DEFAULTS
			),
			'sex' => self::DEFAULTS
		)
    );
        
	protected static $_data = array(
		'children' => array(
			'fields' => array(
				'gender'	=> array(),
				'name'		=> array(
					'min_length' => 1,
					'max_length' => 45
				),
				'pet_name'	=> array(
					'min_length' => 0,
					'max_length' => 45
				),
				'birthdate'	=> array()
			)
		),
        'post' => array(
			'paginator' => array(
				'page_range' => 10,
				'item_count_per_page' => 5
			),
			'preview' => array(
				'length' => 200
			)
        ),
        'news' => array(
			'paginator' => array(
				'page_range' => 10,
				'item_count_per_page' => 5
			),
			'preview' => array(
				'length' => 200
			),
			'rss' => array(
				'count' => 20
			)
        ),
		'site' => array(
			'ajax' => array(
				'protected_error_message' => 'Внутренняя ошибка сервера'
			),
			'sex' => array(
				'male' => 1,
				'female' => 0
			)
		)
	);
	
	public static function getSession() {
		return NULL;
	}
	
	public static function getDatabase() {
		return NULL;
	}
	
	public static function getConfig() {
		return NULL;
	}
	
	public static function getDefaults() {
		$path = func_get_args();
		if (is_array($path[0])) {
			$path = $path[0];
		}

		$current_data = &self::$_data;
		
		while ($path) {
			$path_item = array_shift($path);

			if(!isset ($current_data[$path_item])) {
				throw new Exception('Invalid path');
			}
			$current_data = &$current_data[$path_item];
		}
		
		return $current_data;
	}

	public static function get() {
		$path = func_get_args();
		if (is_array($path[0])) {
			$path = $path[0];
		}

		$old_path = $path;

		$current_data = &self::$_target;
		
		while (is_array($current_data)) {
			$path_item = array_shift($path);

			if(!isset ($path_item) OR !isset ($current_data[$path_item])) {
				throw new Exception('Invalid path');
			}
			$current_data = &$current_data[$path_item];
		}

		switch ($current_data) {
		 case self::DEFAULTS:
			return self::getDefaults($old_path);
		 case self::SESSION:
			return self::getSession($old_path);
		 case self::DATABASE:
			return self::getDatabase($old_path);
		 case self::CONFIG:
			return self::getConfig($old_path);
		 default:
			throw new Exception('Unsupported target type');
		}
	}
	
}